/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
define(["require", "exports", "path"], function (require, exports, path) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    function isImplicitProjectConfigFile(configFileName) {
        return configFileName.indexOf('/dev/null/') === 0;
    }
    exports.isImplicitProjectConfigFile = isImplicitProjectConfigFile;
    function inferredProjectConfig(config) {
        const base = {
            module: 'commonjs',
            target: 'es2016',
            jsx: 'preserve',
            checkJs: false,
            experimentalDecorators: false
        };
        if (config.checkJs) {
            base.checkJs = true;
        }
        if (config.experimentalDecorators) {
            base.experimentalDecorators = true;
        }
        return base;
    }
    exports.inferredProjectConfig = inferredProjectConfig;
    function inferredProjectConfigSnippet(config, vscode) {
        const baseConfig = inferredProjectConfig(config);
        const compilerOptions = Object.keys(baseConfig).map(key => `"${key}": ${JSON.stringify(baseConfig[key])}`);
        return new vscode.SnippetString(`{
	"compilerOptions": {
		${compilerOptions.join(',\n\t\t')}$0
	},
	"exclude": [
		"node_modules",
		"**/node_modules/*"
	]
}`);
    }
    function openOrCreateConfigFile(isTypeScriptProject, rootPath, config, vscode) {
        return __awaiter(this, void 0, void 0, function* () {
            const configFile = vscode.Uri.file(path.join(rootPath, isTypeScriptProject ? 'tsconfig.json' : 'jsconfig.json'));
            const col = vscode.window.activeTextEditor ? vscode.window.activeTextEditor.viewColumn : undefined;
            try {
                const doc = yield vscode.workspace.openTextDocument(configFile);
                return vscode.window.showTextDocument(doc, col);
            }
            catch (_a) {
                const doc = yield vscode.workspace.openTextDocument(configFile.with({ scheme: 'untitled' }));
                const editor = yield vscode.window.showTextDocument(doc, col);
                if (editor.document.getText().length === 0) {
                    yield editor.insertSnippet(inferredProjectConfigSnippet(config, vscode));
                }
                return editor;
            }
        });
    }
    exports.openOrCreateConfigFile = openOrCreateConfigFile;
});
